package cn.kmbeast.service.impl;

import cn.kmbeast.mapper.MessageMapper;
import cn.kmbeast.mapper.UserMapper;
import cn.kmbeast.pojo.api.ApiResult;
import cn.kmbeast.pojo.api.Result;
import cn.kmbeast.pojo.dto.query.extend.MessageQueryDto;
import cn.kmbeast.pojo.em.IsReadEnum;
import cn.kmbeast.pojo.em.MessageType;
import cn.kmbeast.pojo.em.RoleEnum;
import cn.kmbeast.pojo.entity.Message;
import cn.kmbeast.pojo.entity.User;
import cn.kmbeast.pojo.vo.MessageVo;
import cn.kmbeast.service.MessageService;
import lombok.val;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private MessageMapper messageMapper;
    @Resource
    private UserMapper userMapper;


    /**
     * 所有消息发送的基类 实现信息的存储
     * @param messages
     * @return
     */
    @Override
    public Result<Void> batchSave(List<Message> messages) {
        messageMapper.batchSave(messages);
        return ApiResult.success();
    }

    /**
     * 收到评论被回复
     * @param message 评论的消息
     * @return
     */
    @Override
    public Result<Void> evaluationsReplySave(Message message) {
        List<Message> messageList = new ArrayList<>();
        message.setMessageType(MessageType.EVALUATIONS_BY_REPLY.getType());
        message.setContent("你的评论被回复了");
        message.setIsRead(IsReadEnum.READ_NO.getStatus());
        message.setCreateTime(LocalDateTime.now(ZoneId.systemDefault()));
        messageList.add(message);
        return batchSave(messageList);
    }

    /**
     * 收到点赞消息
     * @param message  点赞的消息
     * @return
     */
    @Override
    public Result<Void> evaluationsUpvoteSave(Message message) {
        message.setMessageType(MessageType.EVALUATIONS_BY_UPVOTE.getType());
        message.setContent("你收到了一个点赞");
        message.setIsRead(IsReadEnum.READ_NO.getStatus());
        message.setCreateTime(LocalDateTime.now(ZoneId.systemDefault()));
        List<Message> messageList = new ArrayList<>();
        messageList.add(message);
        return batchSave(messageList);
    }

    /**
     * 系统通知
     * @param messages  系统发送的消息
     * @return
     */
    @Override
    public Result<Void> systemInfoSave(List<Message> messages) {
        messages.forEach(message -> {
            message.setMessageType(MessageType.SYSTEM_INFO.getType());
            message.setIsRead(IsReadEnum.READ_NO.getStatus());
            message.setCreateTime(LocalDateTime.now(ZoneId.systemDefault()));
        });
        return batchSave(messages);
    }

    /**
     * 健康模型警告通知
     * @param messages 健康模型警告消息
     * @return
     */
    @Override
    public Result<Void> dataWordSave(List<Message> messages) {
        messages.forEach(message -> {
            message.setMessageType(MessageType.DATA_MESSAGE.getType());
            message.setIsRead(IsReadEnum.READ_NO.getStatus());
            message.setCreateTime(LocalDateTime.now(ZoneId.systemDefault()));
        });
        return batchSave(messages);
    }

    /**
     * 系统给全部用户通知
     * @param message
     * @return
     */
    @Override
    public Result<Void> systemInfoUsersSave(Message message) {
        //拿到具体的内容
        String content = message.getContent();
        System.out.println(content);
        //发送给所有用户的message集合
        List<Message> messages = new ArrayList<>();
        
        //1.查询全部用户
        List<User> userList = userMapper.query(null);
        for (User user : userList) {
            //排除管理员 并且将信息加入到集合中去
            if (user.getUserRole() != RoleEnum.ADMIN.getRole()){
                Message msg = Message.builder()
                        .content(content)
                        .messageType(MessageType.SYSTEM_INFO.getType())
                        .receiverId(user.getId())
                        .isRead(IsReadEnum.READ_NO.getStatus())
                        .createTime(LocalDateTime.now(ZoneId.systemDefault()))
                        .build();
                messages.add(msg);
            }
           
        }
        //2.批量发送
        messageMapper.batchSave(messages);
        return ApiResult.success();
    }

    /**
     * 清除消息 设置为已读
     * @param messageQueryDto 根据用户Id
     * @return result
     */
    @Override
    public Result<Void> clearMessage(MessageQueryDto messageQueryDto) {
        messageMapper.clearMessage(messageQueryDto);
        return ApiResult.success();
    }

    /**
     * 批零删除
     * @param ids
     * @return
     */
    @Override
    public Result<Void> batchDelete(List<Long> ids) {
        messageMapper.batchDelete(ids);
        return ApiResult.success();
    }

    /**
     * 分页查询  条件查询
     * @param messageQueryDto
     * @return
     */
    @Override
    public Result<List<MessageVo>> query(MessageQueryDto messageQueryDto) {
        List<MessageVo> query = messageMapper.query(messageQueryDto);
        if (query != null && !query.isEmpty()) {
            return ApiResult.success(query, messageMapper.queryCount(messageQueryDto));
        }
        return ApiResult.error("查询为空");
    }
}
